/**
 * 
 * The player manager manages everything related to players for the game.
 * 
 * Author: Matthias Van Eeghem <matthias@van-eeghem.com>
 * All rights reserved.
 * 
 */

package com.bomberman.player;

import java.util.ArrayList;

import com.bomberman.Main;
import com.bomberman.core.Game;
import com.bomberman.core.Position;

public class PlayerManager 
{
	/**
	 * Holds the maximum amount of players for the game 
	 */
	public static int MAX_PLAYERS = 4;
	
	/**
	 * Holds the amount of currently connected players
	 */
	private static int playerCount;
	/**
	 * List of all players that are currently playing (multiplayer support!)
	 */
	private static ArrayList<Player> playerList;
	
	/**
	 * Initialise the server manager
	 * 
	 * @param mainReference
	 */
	public static void initialise(Main mainReference)
	{
		playerList = new ArrayList<Player>();
	}
	
	/**
	 * This method returns a spawn position for a playerid
	 * 
	 * @param playerID
	 * @return
	 */
	public static Position getSpawnPositionForID(int playerID)
	{
		Position position = new Position(-1, -1);
		
		if(playerID == 0)
		{
			position = new Position(0, 0);
		}
		
		// Set the position for player 1
		if(playerID == 1)
		{
			position = new Position(Game.AMOUNT_OF_ROWS - 1, Game.AMOUNT_OF_COLUMNS - 1);
		}
		
		// Set the position for player 1
		if(playerID == 2)
		{
			position = new Position(0, Game.AMOUNT_OF_COLUMNS - 1);
		}
		
		// Set the position for player 1
		if(playerID == 3)
		{
			position = new Position(Game.AMOUNT_OF_ROWS - 1, 0);
		}
		
		return position;
	}

	/**
	 * Add the player object to the list
	 * 
	 * @param name
	 * @param row
	 * @param column
	 * @param playerID
	 */
	public static void addPlayer(String name, int row, int column, int playerID)
	{
		// Make sure we're not breaching the player limit
		if(playerCount <= MAX_PLAYERS)
		{
			// Increment the playercount
			playerCount++;
			
			// Add the new playerS
			playerList.add(new Player(name, row, column, playerID));
		}
		else
		{
			System.out.println("Error: breaching player limit.");
		}
	}
	
	/**
	 * Returns an empty slot for a new player
	 * 
	 * @return
	 */
	public static int getEmptySlot()
	{
		// If the player list size is equal to MAX_PLAYERS, find an empty slot
		if(playerList.size() == MAX_PLAYERS)
		{
			// Search an empty slot
			for(int i = 0; i < MAX_PLAYERS; i++)
			{
				if(playerList.get(i) == null)
					return i;
			}
		}
		
		// The player list size isn't equal to MAX_PLAYERS, just return the list size
		else
		{
			return playerList.size();
		}
		
		// No valid slot found, server is full, return -1
		return -1;
	}
	
	/**
	 * This method returns whether or not the name is in use already
	 * 
	 * @param name
	 * @return
	 */
	public static boolean isNameInUse(String name)
	{
		// Loop through all players and see if there's a match
		for(Player player : playerList)
		{
			if(player.getName().equals(name))
				return true;
		}
		
		return false;
	}
	
	/**
	 * This method returns the player object from a player ID
	 * 
	 * @param ID
	 * @return
	 */
	public static Player getPlayerFromID(int ID)
	{
		// Loop through all players and see if there's a match
		for(Player player : playerList)
		{
			if(player.getID() == ID)
				return player;
		}

		// No valid player object found, return null
		return null;
	}
	
	/**
	 * This method returns the player object from a player name
	 * 
	 * @param ID
	 * @return
	 */
	public static Player getPlayerFromName(String name)
	{
		// Loop through all players and see if there's a match
		for(Player player : playerList)
		{
			if(player.getName().equals(name))
				return player;
		}
		
		// No valid player object found, return null
		return null;
	}
	
	/**
	 * Return all online players 
	 * 
	 * @return
	 */
	public static String getPlayerAsString()
	{
		String playersAsString = "SPAWN_PLAYERS ";
		
		// Loop through all players
		for(Player player : playerList)
		{
			playersAsString += player.getName() + "," + player.getRows().get(0) + "," + player.getColumns().get(0) + "," + player.getID() + " ";
		}
		
		return playersAsString;
	}
	
	/**
	 * Returns the player list.
	 * 
	 * @return The player list
	 */
	
	public static ArrayList<Player> getPlayerList()
	{
		return playerList;
	}
}
